num = int(input())
def is_prime(num):
    if num < 2:
        return False
    for i in range(2,int(num **0.5)+1):
        if num %i ==0:
            return False
        return True
def largest_prime(num):
    if len(str(num))!=4:
        return 0
num_list = sorted(str(num),reverse=True)
for i in range(4,0,-1):
 permuted_num = int(join(num_list[:i]))
 if is_prime(permuted_num):
    return permuted_num


input_number = int(input())
result = largest_prime(input_number)
print(result)